package com.xl.studentpass.controller;

import com.xl.studentpass.entity.po.PostingDetailPo;
import com.xl.studentpass.entity.po.PostingInfoPo;
import com.xl.studentpass.entity.vo.CommunityVo;
import com.xl.studentpass.service.CommunityService;
import com.xl.studentpass.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: xl
 * @createTime: 2023/5/29 20:08:01
 * @program: student-pass
 * @e-mail: 2199396150@qq.com
 * @description:
 */
@RestController
@Slf4j
@RequestMapping("/community")
@Api(tags = "社区功能接口")
public class CommunityController {

    @Resource
    CommunityService communityService;

    /**
     * 获取帖子列表
     */
    @GetMapping("/list")
    @ApiOperation("获取帖子列表")
    public R getList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                     @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        log.info("CommunityController ============获取帖子列表");
        try {
            List<PostingInfoPo> postingInfoPos = communityService.getList(pageNo, pageSize);
            return R.ok().put("postingInfoPos", postingInfoPos);
        } catch (Exception e) {
            log.error("调用获取帖子列表时出错" + e.getMessage());
            return R.error();
        }
    }

    /**
     * 发布帖子
     */
    @PostMapping("/create")
    @ApiOperation("发布帖子")
    public R releasePosting(@RequestBody CommunityVo communityVo) {
        log.info(this.getClass().getSimpleName() + "=============发布帖子");
        if (ObjectUtils.isEmpty(communityVo)) {
            return R.error("创建帖子失败");
        }
        Integer result = communityService.releasePosting(communityVo);
        if (ObjectUtils.isEmpty(result) || result <= 0) {
            return R.error();
        }
        return R.ok();
    }

    /**
     * 修改帖子
     */
    @PostMapping("/alter")
    @ApiOperation("修改帖子")
    public R alterPosting(@RequestBody CommunityVo communityVo) {
        log.info(this.getClass().getSimpleName() + "===============修改帖子");
        if (ObjectUtils.isEmpty(communityVo)) {
            return R.error();
        }
        Integer result = communityService.alterPosting(communityVo);

        if (ObjectUtils.isEmpty(result) || result <= 0) {
            return R.error();
        }
        return R.ok();
    }

    /**
     * 删除帖子
     */
    @PostMapping("/delete")
    @ApiOperation("删除帖子")
    public R deletePosting(@RequestParam("userId") Integer userId, @RequestParam("communityId") Integer communityId) {
        log.info(this.getClass().getSimpleName() + "=================删除帖子");
        if (ObjectUtils.isEmpty(communityId)) {
            return R.error();
        }
        Integer result = communityService.deletePosting(userId, communityId);
        if (ObjectUtils.isEmpty(result) || result <= 0) {
            return R.error();
        }
        return R.ok();
    }

    /**
     * 获取帖子详情
     */
    @GetMapping("/getPostingDetailById")
    @ApiOperation("获取帖子详情")
    public R getPostingDetailById(@RequestParam("communityId") Integer communityId) {
        log.info(this.getClass().getSimpleName() + "=================获取帖子详情");
        if (ObjectUtils.isEmpty(communityId)) {
            return R.error();
        }
        try {
            PostingDetailPo postingDetail = communityService.getPostingDetailById(communityId);
            return R.ok().put("postingDetail", postingDetail);
        } catch (Exception e) {
            log.error("调用帖子详情接口时出错" + e.getMessage());
            return R.error();
        }
    }
}
